// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.

// Copyright (C) 2012	stfour

#include <stdarg.h>
#include "grlib.h"

static void CheckForTile (GRRLIB_texImg * tex)
	{
	if (!tex->tiledtex)
		{
		int w = tex->w / 3;
		int h = tex->h / 3;

		GRRLIB_InitTileSet (tex, w, h, 0);
		}
	}

/* TILES LAYOUT

|---|---|---|
| 0 | 1 | 2 |
|---|---|---|
| 3 | 4 | 5 |
|---|---|---|
| 6 | 7 | 8 |
|---|---|---|

*/

// magpx/y allow to expand image...
// flag is for future use
// tex is the tiled texture
// texbk is the background texture... can be NULL, otherwise it will be used to fill the square...

void grlib_DrawSquareThemed ( s_grlibobj *b, GRRLIB_texImg * tex, GRRLIB_texImg * texbk, int magx, int magy, u32 flag)
	{
	if (tex == NULL) return;

	f32 tw, th;
	s_grlibobj gb;

	CheckForTile (tex);

	grlib_MagObject (&gb, b, magx, magy);

	tw = tex->tilew;
	th = tex->tileh;

	// Upper left
	grlib_DrawTile (gb.x1, gb.y1, tw, th, tex, 0, RGBA (255,255,255,255), 0);
	// Upper right
	grlib_DrawTile (gb.x2-tw, gb.y1, tw, th, tex, 0, RGBA (255,255,255,255), 2);
	// Upper center
	grlib_DrawTile (gb.x1+tw, gb.y1, gb.x2-gb.x1 - (tw*2.0), th, tex, 0, RGBA (255,255,255,255), 1);

	// Lower left
	grlib_DrawTile (gb.x1, gb.y2-th, tw, th, tex, 0, RGBA (255,255,255,255), 6);
	// Lower right
	grlib_DrawTile (gb.x2-tw, gb.y2-th, tw, th, tex, 0, RGBA (255,255,255,255), 8);
	// Lower center
	grlib_DrawTile (gb.x1+tw, gb.y2-th, gb.x2-gb.x1 - (tw*2.0), th, tex, 0, RGBA (255,255,255,255), 7);

	// center left
	grlib_DrawTile (gb.x1, gb.y1+th, tw, gb.y2-gb.y1 - (th*2.0), tex, 0, RGBA (255,255,255,255), 3);
	// center right
	grlib_DrawTile (gb.x2-tw, gb.y1+th, tw, gb.y2-gb.y1 - (th*2.0), tex, 0, RGBA (255,255,255,255), 5);

	// center/center
	if (texbk == NULL)
		grlib_DrawTile (gb.x1+tw, gb.y1+th, gb.x2-gb.x1 - (tw*2.0), gb.y2-gb.y1 - (th*2.0), tex, 0, RGBA (255,255,255,255), 4);
	else
		{
		// Applichiamo la texture come sfondo

		int tbw, tbh;
		int bx, by, bw, bh;

		if (flag == DSTF_BKFILLBORDER)
			{
			bx = gb.x1+1;
			by = gb.y1+th;
			bw = gb.x2 - gb.x1 - 2;
			bh = gb.y2-gb.y1 - (th*2.0)+1;
			}
		else
			{
			bx = gb.x1+tw;
			by = gb.y1+th;
			bw = gb.x2-gb.x1 - (tw*2.0)+1;
			bh = gb.y2-gb.y1 - (th*2.0)+1;
			}

		if (texbk->w > bw)
			tbw = bw;
		else
			tbw = texbk->w;

		if (texbk->h > bh)
			tbh = bh;
		else
			tbh = texbk->h;

		grlib_DrawPart (bx, by, bw, bh, 0, 0, tbw, tbh, texbk, 0, RGBA (255,255,255,255));
		}


	}

